﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.good
{
    /// <summary>
    /// 商品分类（新设计）
    /// </summary>
    public partial class good_type_list : SF.Web.UI.ManagePage
    {
        protected string dis = "display:none;", distype = "display:none;";
        private int isShow;
        private string keywords;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.isShow = DTRequest.GetQueryInt("isShow");
            this.keywords = DTRequest.GetQueryString("keywords");
            if (!Page.IsPostBack)
            {
                //good_type_list 值取至数据库 表示 该模块的调用
                ChkAdminLevel("good_type_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                Model.manager mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
                string busnieseNum = mgr.busnieseNum.ToString();
                string appNum = Session[DTKeys.SESSION_APP_INFO].ToString();
                string templatesNum = Session[DTKeys.SESSION_TEMPLATES_INFO].ToString();
                if (string.IsNullOrEmpty(templatesNum))
                {
                    JscriptMsg("请先在商品管理中选择模版！", "/admin/modeconfig/malltemplet_list.aspx");
                    return;
                }
                Model.sf_modeconfig templateModel = new BLL.sf_modeconfig().GetModel("settingNum='" + templatesNum + "'");
                if (templateModel.marketing_type == 1)
                {
                    dis = "display:block;";
                }
                if (templateModel.goodstype_onshelf.Equals("clothes") || templateModel.goodstype_onshelf.Equals("trousers") || templateModel.goodstype_onshelf.Equals("shoes"))
                {
                    distype = "display:block;";
                }
                RptBind(this.isShow, this.keywords, " and " + Utils.getWhereByInfo(busnieseNum, appNum, templatesNum) + CombSqlTxt(this.keywords));
            }
        }

        //数据绑定
        private void RptBind(int _isShow, string _keywords, string _strWhere)
        {
            //加入商户,公众号 等条件
            ddlIsShow.SelectedValue = _isShow.ToString();
            txtKeywords.Text = _keywords;
            if (_isShow != 0)
                _strWhere = " isShow=" + (_isShow == 2 ? 0 : _isShow) + _strWhere;
            else
                _strWhere = " 1=1 " + _strWhere;
            BLL.bs_good_type bll = new BLL.bs_good_type();
            DataTable dt = bll.GetList(0, _strWhere);
            this.rptList.DataSource = dt;
            this.rptList.DataBind();
        }
        #region 组合SQL查询语句
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (name like '%"
                    + _keywords + "%' or remark like '%" + _keywords + "%' ) ");
            }
            return strTemp.ToString();
        }
        #endregion
        //美化列表
        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal LitFirst = (Literal)e.Item.FindControl("LitFirst");
                HiddenField hidLayer = (HiddenField)e.Item.FindControl("hidLayer");
                string LitStyle = "<span style=\"display:inline-block;width:{0}px;\"></span>{1}{2}";
                string LitImg1 = "<span class=\"folder-open\"></span>";
                string LitImg2 = "<span class=\"folder-line\"></span>";
                int classLayer = Convert.ToInt32(hidLayer.Value);
                if (classLayer == 1)
                {
                    LitFirst.Text = LitImg1;
                }
                else
                {
                    LitFirst.Text = string.Format(LitStyle, (classLayer - 2) * 24, LitImg2, LitImg1);
                }
            }
        }

        //保存排序
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("good_type_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
            BLL.bs_good_type bll = new BLL.bs_good_type();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort=" + sortId.ToString());
            }
            if (rptList.Items.Count > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "保存商品分类排序"); //记录日志
                JscriptMsg("保存排序成功！", Utils.CombUrlTxt("good_type_list.aspx", "", ""));
            }
        }

        protected void ddlIsShow_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("good_type_list.aspx", "isShow={0}&keywords={1}",
                 ddlIsShow.SelectedValue, this.keywords));
        }
        //关健字查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("good_type_list.aspx", "isShow={0}&keywords={1}",
                this.isShow.ToString(), txtKeywords.Text));
        }

        //删除类别
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("good_type_list", DTEnums.ActionEnum.Delete.ToString()); //检查权限
            BLL.bs_good_type bll = new BLL.bs_good_type();
            BLL.bs_good_type_field fieldBll = new BLL.bs_good_type_field();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    bll.Delete(id);
                    fieldBll.DeleteByGoodTypeId(id);  //删除类别下的字段定义
                }
            }
            AddAdminLog(DTEnums.ActionEnum.Delete.ToString(), "删除商品分类分类数据"); //记录日志
            JscriptMsg("删除数据成功！", Utils.CombUrlTxt("good_type_list.aspx", "isShow={0}&keywords={1}", this.isShow.ToString(), this.keywords));
        }


        //&&上架
        protected void btnOnShelf_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("good_type_list", DTEnums.ActionEnum.OnShelf.ToString()); //检查权限
            int sucCount = 0; //成功数量
            int errorCount = 0; //失败数量
            BLL.bs_good_type bll = new BLL.bs_good_type();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.UpdateField(id, " isShow=1 "))
                    {
                        sucCount++;
                    }
                    else
                    {
                        errorCount++;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.OnShelf.ToString(), "上架商品分类内容成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("上架商品分类成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("good_type_list.aspx", "isShow={0}&keywords={1}", this.isShow.ToString(), this.keywords));
        }
        //&&批量下架 done
        protected void btnOffShelf_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("good_type_list", DTEnums.ActionEnum.OffShelf.ToString()); //检查权限
            int sucCount = 0; //成功数量
            int errorCount = 0; //失败数量
            BLL.bs_good_type bll = new BLL.bs_good_type();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.UpdateField(id, " isShow=0 "))
                    {
                        sucCount++;
                    }
                    else
                    {
                        errorCount++;
                    }
                }
            }
            AddAdminLog(DTEnums.ActionEnum.OffShelf.ToString(), "下架商品分类内容成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("下架商品分类成功" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("good_type_list.aspx", "isShow={0}&keywords={1}", this.isShow.ToString(), this.keywords));
        }

        /// <summary>
        /// 分类商品字段定义
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnGoodTypeField_Click(object sender,EventArgs e)
        {
            //JscriptMsg("分类商品字段定义", Utils.CombUrlTxt("good_type_list.aspx", "isShow={0}&keywords={1}", this.isShow.ToString(), this.keywords));

            var dt = rptList.DataSource as DataTable;
            var newRow = dt.NewRow();
            newRow["typeName"] = "新增";
            dt.Rows.Add(newRow);

            var items = rptList.Items.Count;
            JscriptMsg(items.ToString(), Utils.CombUrlTxt("good_type_list.aspx", "isShow={0}&keywords={1}", this.isShow.ToString(), this.keywords));
        }
    }
}